#!/bin/bash
. env.sh

mkdir -p $BUILD_ROOT
mkdir -p $BUILD_ROOT/obj
mkdir -p $BIN_ROOT
mkdir -p $SOURCE_DIR/lib

if [ $# == 1 ];then
	if [ $1 == 'clean' ];then
		rm -rf $BUILD_ROOT/obj/*
		rm -rf $BUILD_ROOT/bin/tracker
		rm -rf $BUILD_ROOT/bin/storage
	elif [ $1 == 'conf' ];then
		export TARGET_BIN=conf
		mkdir -p $BUILD_ROOT/obj/$TARGET_BIN
		cd sdfs/config;
		make;
		make -f makeobj;
	elif [ $1 == 'file' ];then
		export TARGET_BIN=fileServer
		cd sample/fileTransport;
		make;
		make -f makeobj;
	elif [ $1 == 'tracker' ];then
		export TARGET_BIN=tracker
		mkdir -p $BUILD_ROOT/obj/$TARGET_BIN
		cd sdfs/protobuf;
		make;
		cd ../tracker;
		make;
		make -f makeobj;
	elif [ $1 == 'test' ];then
		export TARGET_BIN=test;
		mkdir -p $BUILD_ROOT/obj/$TARGET_BIN;
		cd sdfs/protobuf;
		make;
		cd ../test;
		make;
		make -f makeobj;
	elif [ $1 == 'storage' ];then
		export TARGET_BIN=storage
		mkdir -p $BUILD_ROOT/obj/$TARGET_BIN
		cd sdfs/protobuf;
		make;
		cd ../storage;
		make;
		make -f makeobj;
	else
		export TARGET_BIN=$1;
		mkdir -p $BUILD_ROOT/obj/$TARGET_BIN;
		cd sdfs/test/$1;
		./build.sh $1;
	fi
elif [ $# = 2 ];then
	if [ $1 == 'clean' ];then
		if [ $2 == 'storage' ];then
			rm -rf $BUILD_ROOT/obj/storage;
			rm -f $BUILD_ROOT/bin/storage;
		elif [ $2 == 'tracker' ];then
			rm -rf $BUILD_ROOT/obj/tracker;
			rm -f $BUILD_ROOT/bin/tracker;
		fi
	fi
else
	echo "build.sh [clean][tracker][storage]"
fi
